package com.topboya.linker.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.model.response.FileBytaskIdAndProductIdReponse;
import com.topboya.linker.model.response.HomeListResponse;
import com.topboya.linker.model.response.ProductInforListReponse;
import com.topboya.linker.model.response.TaskFileResponse;
import com.topboya.linker.model.response.TaskProductResponse;
import com.topboya.linker.ui.adapter.SeeSelectProductAdapter;
import com.topboya.linker.ui.adapter.SeeTaskProductAdapter;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.CustomerPresenter;
import com.topboya.linker.ui.presenter.SeeTaskInfoAtPresenter;
import com.topboya.linker.ui.view.CustomerView;
import com.topboya.linker.ui.view.SeeTaskInfoAtView;

import java.util.ArrayList;
import java.util.List;

//查看已经选择的产品信息
public class SeeTaskProductActivity extends BaseActivity<SeeTaskInfoAtView, SeeTaskInfoAtPresenter> implements SeeTaskInfoAtView {

    private static final String TAG = "SeeSelectedProductActivity:";
    private TextView toolbar_title;
    private TextView tv_toolbar_right;
    private Toolbar toolbar;
    private RecyclerView rey_seetaskproduct;
    private SeeTaskProductAdapter seeTaskProductAdapter;
    private ArrayList<TaskProductResponse.Data> data;
    private HomeListResponse.TaskList task;

    @Override
    protected int getLayoutId() {

        return R.layout.activity_see_select_productinfo;
    }

    @Override
    protected void initView() {

        toolbar_title = findViewById(R.id.toolbar_title);
        rey_seetaskproduct = findViewById(R.id.rey_seeselectproduct);
        tv_toolbar_right = findViewById(R.id.tv_toolbar_right);
        toolbar = findViewById(R.id.toolbar);
    }

    @Override
    protected void initData() {

        toolbar_title.setText("产品列表");
        tv_toolbar_right.setVisibility(View.GONE);
        task = (HomeListResponse.TaskList) getIntent().getSerializableExtra("task");
        data = new ArrayList<>();
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rey_seetaskproduct.setLayoutManager(layoutManager);
//        rey_seeselectproduct.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, 2, 0xBFC0C2));
        seeTaskProductAdapter = new SeeTaskProductAdapter(this, data);
        rey_seetaskproduct.setAdapter(seeTaskProductAdapter);
       mPresenter.getTaskProductList(task.getId());

    }

    @Override
    protected void initListener() {

        toolbar.setNavigationOnClickListener(v -> { finish();});
        seeTaskProductAdapter.setOnItemClickListener((v, position) -> {

            Intent intent = new Intent(SeeTaskProductActivity.this,SeeTaskFileActivity.class);
            intent.putExtra("task",task);
            intent.putExtra("productID",data.get(position).getProductId());
            intent.putExtra("istaskFile",false);
            startActivity(intent);

        });


    }

    @Override
    protected SeeTaskInfoAtPresenter createPresenter() {

        return new SeeTaskInfoAtPresenter(this);
    }

    //   重写返回键，怕用户点删除按钮了，直接返回
    @Override
    public void onBackPressed() {

        back();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 是否触发按键为back键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        } else { // 如果不是back键正常响应
            return super.onKeyDown(keyCode, event);
        }
    }

    private void back() {

        finish();

    }

    @Override
    public void getTaskProductList(List<TaskProductResponse.Data> taskProductResponse) {

        data.addAll(taskProductResponse);
        seeTaskProductAdapter.notifyDataSetChanged();
    }

    @Override
    public void getTaskFile(List<TaskFileResponse.Data> taskfilelist) {

    }

    @Override
    public void getFeekPruductFileBytaskIdAndProductId(List<FileBytaskIdAndProductIdReponse.FileList> filebytaskidlist) {

    }
}
